package com.toune.movie.mvp.model;

import android.util.Log;

import com.toune.movie.base.Api;
import com.toune.movie.mvp.contract.VarietyDetailContract;
import com.toune.movie.mvp.presenter.VarietyDetailPresenter;
import com.toune.movie.vo.VarietyDetailVo;
import com.vondear.rxtool.RxThreadPoolTool;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/5/15.
 */

public class VarietyDetailModel implements VarietyDetailContract.Model {
    VarietyDetailPresenter varietyPresenter;

    public VarietyDetailModel(VarietyDetailPresenter varietyPresenter) {
        this.varietyPresenter = varietyPresenter;
    }

    @Override
    public void getData(final String url, final int tag) {
        new RxThreadPoolTool(RxThreadPoolTool.Type.CachedThread, 1).execute(new Runnable() {
            @Override
            public void run() {
                if (tag == 0) {
                    paseHtml(url);
                } else if (tag == 1) {
                    paseHtmlTv(url);
                } else if (tag == 2) {
                    paseHtmlVariety(url);
                }else {
                    paseHtmlComic(url);
                }
            }
        });
    }

    private void paseHtmlComic(String url) {
        try {
            VarietyDetailVo varietyDetailVo = new VarietyDetailVo();
            //还是一样先从一个URL加载一个Document对象。
            Document doc = Jsoup.connect(url).get();
            //视频播放地址
            Elements iframe = doc.getElementsByTag("iframe");
            String src = iframe.get(0).attr("src");
            varietyDetailVo.setImageUrl(src);
            Element dianshijuid = doc.getElementById("dianshijuid");
            Elements lis = dianshijuid.getElementsByTag("a");
            List<VarietyDetailVo.SourceList> sourceLists = new ArrayList<>();
            VarietyDetailVo.SourceList sourceList = new VarietyDetailVo.SourceList();
            List<VarietyDetailVo.SourceList.DataBean> dataBeanList = new ArrayList<>();
            for (int i = 0; i < lis.size(); i++) {
                VarietyDetailVo.SourceList.DataBean dataBean = new VarietyDetailVo.SourceList.DataBean();
                Element element = lis.get(i);
                String text = element.text();
                dataBean.setName(text);
                String detailUrl = element.attr("href");
                dataBean.setUrl(detailUrl);
                dataBeanList.add(dataBean);
            }
            sourceList.setDataBeanList(dataBeanList);
            sourceLists.add(sourceList);
            varietyDetailVo.setSourceLists(sourceLists);
            varietyPresenter.setData(varietyDetailVo);
        } catch (Exception e) {
            Log.i("mytag", e.toString());
        }
    }

    private void paseHtmlVariety(String url) {
        try {
            VarietyDetailVo varietyDetailVo = new VarietyDetailVo();
            //还是一样先从一个URL加载一个Document对象。
            Document doc = Jsoup.connect(url).get();
            //视频播放地址
            Elements iframe = doc.getElementsByTag("iframe");
            String src = iframe.get(0).attr("src");
            varietyDetailVo.setImageUrl(src);
            Element dianshijuid = doc.getElementById("dianshijuid");
            Elements lis = dianshijuid.getElementsByTag("li");
            List<VarietyDetailVo.SourceList> sourceLists = new ArrayList<>();
            for (int i = 0; i < lis.size(); i++) {
                VarietyDetailVo.SourceList sourceList = new VarietyDetailVo.SourceList();
                Element element = lis.get(i);
                Elements img = element.getElementsByTag("img");
                String imgUrl = img.get(0).attr("data-src");
                sourceList.setImgUrl(imgUrl);
                String text = img.get(0).attr("alt");
                sourceList.setTitle(text);
                Elements a = element.getElementsByTag("a");
                String detailUrl = a.get(0).attr("href");
                sourceList.setUrl(detailUrl);
                Elements span = element.getElementsByTag("span");
                String expect = span.get(0).text();
                sourceList.setExpect(expect);
                sourceLists.add(sourceList);
            }
            varietyDetailVo.setSourceLists(sourceLists);
            varietyPresenter.setData(varietyDetailVo);
        } catch (Exception e) {
            Log.i("mytag", e.toString());
        }
    }

    private void paseHtmlTv(String url) {
        try {
            VarietyDetailVo varietyDetailVo = new VarietyDetailVo();
            //还是一样先从一个URL加载一个Document对象。
            Document doc = Jsoup.connect(url).get();
            //视频播放地址
            Elements iframe = doc.getElementsByTag("iframe");
            String src = iframe.get(0).attr("src");
            varietyDetailVo.setImageUrl(src);
            Element dianshijuid = doc.getElementById("myouter");
            Element mytab = dianshijuid.getElementById("mytab");
            Elements lis = mytab.getElementsByTag("li");
            List<VarietyDetailVo.SourceList> sourceLists = new ArrayList<>();
            for (int i = 0; i < lis.size(); i++) {
                VarietyDetailVo.SourceList sourceList = new VarietyDetailVo.SourceList();
                Element element = lis.get(i);
                String text = element.text();
                sourceList.setTitle(text);
                sourceLists.add(sourceList);
            }
            Element mycontent = doc.getElementById("mycontent");
            Elements uls = mycontent.getElementsByTag("ul");
            for (int i = 0; i < uls.size(); i++) {
                VarietyDetailVo.SourceList sourceList = sourceLists.get(i);
                List<VarietyDetailVo.SourceList.DataBean> dataBeans = new ArrayList<>();
                Elements as = uls.get(i).getElementsByTag("a");
                for (int j = 0; j < as.size(); j++) {
                    Element element = as.get(j);
                    VarietyDetailVo.SourceList.DataBean dataBean = new VarietyDetailVo.SourceList.DataBean();
//                    String onclick = element.attr("onclick");
//                    String[] split = onclick.split("'");
//                    if (split.length > 1) {
//                        String hrefUrl = split[1];
//                        dataBean.setUrl(hrefUrl);
//                    }
                    String href = element.attr("href");
                    dataBean.setUrl(href);
                    String text = element.text();
                    dataBean.setName(text);
                    dataBeans.add(dataBean);
                }
                sourceList.setDataBeanList(dataBeans);
            }
            varietyDetailVo.setSourceLists(sourceLists);
            varietyPresenter.setData(varietyDetailVo);
        } catch (Exception e) {
            Log.i("mytag", e.toString());
        }
    }

    private void paseHtml(String url) {
        try {
            VarietyDetailVo varietyDetailVo = new VarietyDetailVo();
            //还是一样先从一个URL加载一个Document对象。
            Document doc = Jsoup.connect(url).get();
            //视频播放地址
            Elements iframe = doc.getElementsByTag("iframe");
            String src = iframe.get(0).attr("src");
            varietyDetailVo.setImageUrl(src);
            Element dianshijuid = doc.getElementById("dianshijuid");
            Elements as = dianshijuid.getElementsByTag("a");
            List<VarietyDetailVo.SourceList> varietys = new ArrayList<>();
            for (int i = 0; i < as.size(); i++) {
                VarietyDetailVo.SourceList sourceList = new VarietyDetailVo.SourceList();
                Element element = as.get(i);
                String href = Api.BaseUrl52 + element.attr("href");
                String text = element.text();
                sourceList.setTitle(text);
                sourceList.setUrl(url);
                varietys.add(sourceList);
            }
            varietyDetailVo.setSourceLists(varietys);
            varietyPresenter.setData(varietyDetailVo);
        } catch (Exception e) {
            Log.i("mytag", e.toString());
        }
    }
}
